package com.yn.pet.controller.app.biz;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.github.pagehelper.PageInfo;
import com.yn.common.constant.HttpStatus;
import com.yn.common.core.domain.entity.SysDictData;
import com.yn.common.core.page.TableDataInfo;
import com.yn.common.utils.PageUtils;

import com.yn.pet.controller.pc.response.HelpCenterDetailResponse;
import com.yn.pet.controller.pc.response.HelpCenterResponse;
import com.yn.pet.controller.pc.response.NoticeTypeResponse;
import com.yn.pet.controller.pc.response.page.HelpCenterPageResponse;
import com.yn.system.domain.SysNotice;
import com.yn.system.mapper.SysNoticeMapper;
import com.yn.system.service.ISysDictDataService;
import com.yn.system.service.ISysNoticeService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;



@Service
@Slf4j
@RequiredArgsConstructor
public class AHelpCenterBizService {
    private final ISysDictDataService dictDataService;
    private final ISysNoticeService noticeService;


    /**
     * 用户端分页查询帮助中心
     * @param pageNum 当前页
     * @param pageSize 每页显示数量
     * @param noticeType 通知类型: 3-功能介绍, 4-常见问题
     * @return
     */
    public HelpCenterPageResponse pageList(Integer pageNum, Integer pageSize, Integer noticeType) {
        SysNotice notice=new SysNotice();
        notice.setNoticeType(noticeType.toString());
        PageUtils.startPage();
        List<SysNotice> sysNotices = noticeService.selectNoticeList(notice);
        HelpCenterPageResponse response = new HelpCenterPageResponse();
        ArrayList<HelpCenterResponse> records = new ArrayList<>();
        if (CollectionUtils.isEmpty(sysNotices)){
            response.setRecords(records);
            response.setTotal(0L);
            return response;
        }
        for (SysNotice sysNotice : sysNotices) {
            HelpCenterResponse helpCenterResponse = new HelpCenterResponse();
            helpCenterResponse.setNoticeId(sysNotice.getNoticeId());
            helpCenterResponse.setNoticeTitle(sysNotice.getNoticeTitle());
            helpCenterResponse.setNoticeType(Integer.parseInt(sysNotice.getNoticeType()));
            helpCenterResponse.setNoticeTypeName(dictDataService.selectDictLabel("sys_notice_type", sysNotice.getNoticeType()));
            records.add(helpCenterResponse);
        }
        PageInfo<SysNotice> pageInfo = new PageInfo<>(sysNotices);
        response.setRecords(records);
        response.setTotal(pageInfo.getTotal());
        response.setCurrent(pageInfo.getPageNum());
        response.setSize(pageInfo.getPageSize());
        response.setPages(pageInfo.getPages());
        return response;
    }


    /**
     * 根据dictType查询通知类型
     * @param dictType 字典类型
     * @return List<NoticeTypeResponse>
     */
    public List<NoticeTypeResponse> queryNoticeTypeByDictType(String dictType) {
        List<SysDictData> sysDictDataList = dictDataService.selectDictDataByDictType(dictType);
        if(CollectionUtils.isEmpty(sysDictDataList)){
            return Collections.emptyList();
        }
        return sysDictDataList.stream().map(NoticeTypeResponse::transform).collect(Collectors.toList());
    }

    /**
     * 用户端分页查询帮助中心常用问题详情
     * @param noticeId 公告ID
     * @return
     */
    public HelpCenterDetailResponse queryDetail(Long noticeId) {
        SysNotice notice = noticeService.selectNoticeById(noticeId);
        HelpCenterDetailResponse transform = HelpCenterDetailResponse.transform(notice);
        transform.setNoticeTypeName(dictDataService.selectDictLabel("sys_notice_type", notice.getNoticeType()));
        return transform;

    }

    /**
     * 根据dictType查询通知类型
     * @param dictType 字典类型
     * @return Map<dict_value,dict_label>
     */
    public Map<Integer,String> queryNoticeTypeByDictType(String dictType, String separator) {
        List<SysDictData> sysDictDataList = dictDataService.selectDictDataByDictType(dictType);
        if(CollectionUtils.isEmpty(sysDictDataList)){
            return Collections.emptyMap();
        }
        Map<Integer, String> res = new HashMap<>();
        for (SysDictData sysDictData : sysDictDataList) {
            res.put(Integer.parseInt(sysDictData.getDictValue()),sysDictData.getDictLabel());
        }
        return res;
    }


}
